<?php
class BudgetService
{

    /*
    * 是否是合适的地板主材料
    *
    */
    public static function isVaildGroundMaterial ($type_Id)
    {
        if($type_Id == MsgConst::BUDGET_GROUNDTYPE_BOARD  || $type_Id == MsgConst::BUDGET_GROUNDTYPE_TILE)
            return;

        throw new Exception("非地板主材料id ".$type_Id);


    }

    /*
    * 是否是合适的墙面主材料
    *
    */
    public static function isVaildWallMaterial ($type_Id)
    {
        if($type_Id == MsgConst::BUDGET_WALLTYPE_TILE  || $type_Id == MsgConst::BUDGET_WALLTYPE_PAPER || $type_Id == MsgConst::BUDGET_WALLTYPE_PAINT)
            return;

        throw new Exception("非墙面主材料id ".$type_Id);


    }


    /*
    * 确定case 是否全部填写完毕
    *
    */
    public static function checkCaseStatus($case)
    {
        $inputinfo = $case->input_info;
        $input_array = json_decode($inputinfo);

        $caseid =$case->id;
        $num = 0;

        $flag = true;
        for($i=0;$i<count($input_array);$i++)
        {
            $type_id =  $input_array[$i]->type_id;
            $num = $input_array[$i]->num;

            for($t=0;$t<$num;$t++)
            {

                //查找该案例是否已经有数据
                $searchsql = Util::initSearchSql();

                $searchsql = Util::getSerarchStrAdd($searchsql,"case_id",$caseid);
                $searchsql = Util::getSerarchStrAdd($searchsql,"index_num",$t);
                $searchsql = Util::getSerarchStrAdd($searchsql,"house_type_id",$type_id);

                $obj = BudgetCaseDetail::model()->find($searchsql);

                if(empty($obj))
                {
                    $flag = false;
                    break;
                }

            }

            if($flag==false)
                break;

        }

        if($flag)
        {
            $case->status = MsgConst::BUDGET_STATUS_FINISHINPUT;
            Util::updateObjData($case);
            Util::saveObjData($case);
        }


    }


    /*
    * 获取case detail
    *
    */
    public static function getCaseDetail($case_id,$index,$type_id)
    {
         $obj  = BudgetCaseDetail::model()->find(" is_delete=0 and case_id=:case_id and index_num=:index_num and house_type_id=:house_type_id ",
                     array(":case_id" => $case_id,":index_num" => $index,":house_type_id" => $type_id));

        return $obj;




    }

    /*
    *
    *
    */
    public static function generateRestuleInfo($obj,$case_id)
    {


        $result_info = array();
        $count = 0;
        $inputinfo = $obj->input_info;

        $typearray = json_decode($inputinfo);


        $remark = "";

        for($i=0;$i<count($typearray);$i++)
        {
            $typeobj = $typearray[$i];
            $type_id = $typeobj->type_id;

            $type = BudgetHousetype::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type);

            $num = $typeobj->num;

            if($num>0)
                $remark = $remark.$num.$type->shortname;

            for($t=0;$t<$num;$t++)
            {
                $num_info = array();
                $num_info["count"]=$count;
                $num_info["status"]=MsgConst::BUDGET_STATUS_INIT;

                $num_info["type_id"] = $type_id;
                $num_info["type_name"] = $type->name;
                $num_info["index"] = $t;


                if($case_id>0)
                {
                    $detailobj = BudgetService::getCaseDetail($case_id,$t,$type_id);

                    if(empty($detailobj)==false)
                    $num_info["detailinfo"] = ($detailobj);

                }
                else
                {
                    $num_info["detailinfo"] = "";

                }







                $count++;



                array_push($result_info,$num_info);
            }





        }

        $obj->input_desc = $remark;
       //  $obj->result_info =  json_encode($result_info);
        $obj->result_info =  $result_info;
        return $obj;


    }

    /*
    *
    *
    */
    public static function getCaseIdByUserid($user_id)
    {
        $searchsql = Util::initSearchSql();
        $searchsql = Util::getSerarchStrAdd($searchsql,"user_id",$user_id);

        //排序
        $searchsql = $searchsql . " order by create_time  desc";

         $obj  = BudgetCase::model()->find($searchsql);

        if(empty($obj)==false)
            return $obj->id;

        return -1;


    }


    /*
    * 获取材料列表
    *
    */
    public static function getMaterialList ($case_info)
    {
        //获取大分类列表
        $m_type_list = BudgetService::getFirstMaterialTypeList();


        $return_array = array();



        for($i=0;$i<count($m_type_list);$i++)
        {
            $m_type = $m_type_list[$i];

            //获取小分类列表
            $s_type_list = BudgetService::getSecondMaterialTypeList($m_type->id);

            $s_count = count($s_type_list);

            for($t=0;$t<$s_count;$t++)
            {
                $s_type = $s_type_list[$t];

                //创建种类数组
                 $s_type_array = array();

                $s_type_array["id"]=$s_type->id;
                $s_type_array["name"]=$m_type->name."-".$s_type->name;


                //获取材料列表
                $m_list = BudgetService::getMaterialListByTypeID($s_type->id);



                $return_m_list = array();


                //遍历每个材料

                for($m=0;$m<count($m_list);$m++)
                {
                    $material = $m_list[$m];
                    $material->type_id = $m_type->id;
                    $material = BudgetService::getMaterialNum($case_info,$m_list[$m]);
                    if($material->num > 0)
                        array_push($return_m_list,$material);

                }
                $s_type_array["mlist"] = $return_m_list;

                //加入数据

                array_push($return_array,$s_type_array);




            }





        }

        return $return_array;


    }

    /*
    * 获取一级材料种类列表
    *
    */
    public static function getFirstMaterialTypeList()
    {
        $sql = Util::initSearchSql();
        $sql= Util::getSerarchStrAdd($sql,"level",1);
        $sql = $sql . " order by id  asc";



        $list = BudgetMaterialType::model()->findAll($sql);


        return $list;





    }

    /*
   * 获取二级材料种类列表
   *
   */
    public static function getSecondMaterialTypeList($id)
    {
        $sql = Util::initSearchSql();
        $sql= Util::getSerarchStrAdd($sql,"level",2);
        $sql= Util::getSerarchStrAdd($sql,"parent_id",$id);
        $sql = $sql . " order by id  asc";


        $list = BudgetMaterialType::model()->findAll($sql);

        return $list;





    }

    /*
    *
    * 获取材料列表
    */
    public static function getMaterialListByTypeID($type_id)
    {

        $sql = Util::initSearchSql();
       // $sql= Util::getSerarchStrAdd($sql,"level",2);
        $sql= Util::getSerarchStrAdd($sql,"type_id",$type_id);
        $sql = $sql . " order by id  asc";


        $list = BudgetMaterial::model()->findAll($sql);

        return $list;



    }


    /*
    * 获取材料数量
    *
    */
    public static function getMaterialNum($case_info,$material)
    {

        //主材
        if($material->main_type_id == MsgConst::BUDGET_TYPE_MAIN_ID)
        {
            //地面材料




            //墙面材料
        }


        //辅材

        //手工



        return $material;


    }










}


?>